package com.example.bbs.controller;


import com.example.bbs.entity.Doctor;
import com.example.bbs.entity.User;
import com.example.bbs.service.DiseaseTagService;
import com.example.bbs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @author jiaziyun
 * @since 2021-04-30
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private DiseaseTagService diseaseTagService;

    @RequestMapping("/index")
    public String getUserInfo(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/";
        } else {
            return getUserInfo(String.valueOf(user.getId()), model);
        }
    }

    //个人页
    @RequestMapping("/{id}")
    public String getUserInfo(@PathVariable("id") String id, Model model) {
        model.addAttribute("doctor", new Doctor());
        model.addAttribute("userinfo", new User());

        model.addAttribute("diseasetags", diseaseTagService.selectAllDisease());
        if (id == null || id.isEmpty() || !userService.getUserInfo(id, model)) {
            return "redirect:/";
        } else {
            return "usermanage";
        }
    }

    //个人页更新
    @RequestMapping("/updatebase")
    @ResponseBody
    public String updateBaseinfo(@RequestBody HashMap<String, String> parmmap) {
        if (parmmap == null || parmmap.size() == 0) {
            return "400";
        }
        return userService.updateBaseInfo(parmmap);
    }

}
